package Test;

import DB.CustomerDB;
import Model.Customer;
import org.junit.Test;

import java.util.ArrayList;

import static org.junit.Assert.fail;

public class CustomerDBTest {

    @Test
    public void testInsertCustomer() throws Exception {
        CustomerDB customerDB = new CustomerDB();
        int rc = customerDB.insertCustomer(new Customer(4, "Test customer", "Sofiendalsvej 200", 9000 , "Aalborg", 88888888));

        System.out.println(rc);

        if (rc == 1) {
            System.out.println("Inserting customer successful");
        }
        else {
            fail("Failed inserting customer");
        }
    }

    @Test
    public void testFindCustomer() throws Exception {

        CustomerDB customerDB = new CustomerDB();

        int find = 2;

        Customer customer = customerDB.findCustomer(find);

        if (customer != null) {
            System.out.println(
                    customer.getId() + " " +
                    customer.getName() + " " +
                    customer.getAddress() + " " +
                    customer.getZipCode() + " " +
                    customer.getCity()+ " "+
                    customer.getPhoneNo());
        }
        else {
            fail("Error finding customer - customer is null");
        }
    }

    @Test
    public void testGetAllCustomers() throws Exception {
        CustomerDB customerDB = new CustomerDB();
        ArrayList<Customer> customerList = customerDB.getAllCustomers(false);

        if (customerList != null) {
            for (Customer c : customerList) {
                System.out.println(c.getName());
                System.out.println(c.getCity());
            }
        }
        else {
            fail("No customers found");
        }
    }

    @Test
    public void testUpdateCustomer() throws Exception {
        CustomerDB customerDB = new CustomerDB();
        String updateValue = "TestName";

        Customer customer = new Customer(4, "Test customer", "Sofiendalsvej 200", 9000 , "Aalborg", 88888888);

        customerDB.updateCustomer(customer);

        String customerName = customerDB.findCustomer(10).getName();

        if (customerName.equals(updateValue)) {
            System.out.println("Update successful");
        }
        else {
            fail("Update failed");
        }
    }

    @Test
    public void testDeleteCustomer() throws Exception {
        CustomerDB customerDB = new CustomerDB();
        Customer customer = new Customer(10000, "Delete Me", "Delete Street", 9000 , "Delete City", 00000000);
        customerDB.insertCustomer(customer);

        if (customerDB.findCustomer(1000) != null) {

            customerDB.deleteCustomer(1000);

            if (customerDB.findCustomer(1000) == null) {
                System.out.println("Delete successful");
            }
            else {
                fail("Delete not successful");
            }
        }
        else {
            fail("Inserting for deletion failed");
        }
    }
}
